package com.muk.meis.controller.school;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.annotation.WebServlet;

import com.muk.meis.model.bean.OpecSchool;
import com.muk.meis.model.bean.OpecSetup;
import com.muk.meis.model.service.BaseMEISService;
import com.muk.meis.model.service.OpecSchoolService;
import com.muk.meis.model.service.OpecSetupService;
import com.muk.webframework.framework.controller.BaseController;

/**
 * Servlet implementation class OPECController
 */
@WebServlet("/OPECController/*")
public class OPECController extends BaseController {
	private static final long serialVersionUID = -8505219929713831088L;

	/**
     * @see BaseMEISService#BaseMEISService()
     */
    public OPECController() {}
    
    public void update() throws IOException{
    	String percode = request.getParameter("percode");
    	OpecSetupService opecSetupService = new OpecSetupService();
    	OpecSchoolService opecSchoolService = new OpecSchoolService();
    	opecSchoolService.clearToNew(percode);
    	int n = opecSetupService.getMaxOPECSetupId();
    	List<OpecSchool> lst = new ArrayList<>();
		for (int i = 1; i <= n; i++) {
			String point = request.getParameter("opec_value"+i);
			OpecSchool opecSchool = new OpecSchool();
			opecSchool.setCreateDate(new Date());
			opecSchool.setOpecPoint(Integer.parseInt(point));
			OpecSetup opecSetup = new OpecSetup();
			opecSetup.setOpecId(i);
			opecSchool.setOpecSetup(opecSetup);
			opecSchool.setPercodeCode(percode);
			lst.add(opecSchool);
		}
		opecSchoolService.update(lst);
    	response.sendRedirect(request.getContextPath()+"/school/listnews.jsp?percode="+percode+"#tabs-4");
    }
    
    

}
